package com.jack.payment.service.mongo;

import com.jack.payment.entity.filter.RestFilter;
import com.jack.payment.entity.mong.RequestLogModel;

import com.jack.payment.util.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RequestLogServiceImpl {
    private Logger log = LoggerFactory.getLogger(RequestLogServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;



    /**
     * 写入请求日志
     * @param requestLogModel
     * @param collectionName
     */
    public void saveRequestLog(RequestLogModel requestLogModel, String collectionName) {
            mongoTemplate.insert(requestLogModel, collectionName);
    }

    /**
     *  分页查询出 符合条件的数据列表
     * @param filterModel
     * @return
     */
    public List<RequestLogModel> filterRequestLogRecordList(RestFilter.RequestLogFilter filterModel) {
        Sort.Direction direction  = filterModel.getSort() == null ? Sort.Direction.DESC :
                filterModel.getSort() == 0 ? Sort.Direction.DESC : Sort.Direction.ASC;
        Sort sort = Sort.by(direction, "createTime");
        Query query = requestLogCondition(filterModel);
        System.out.println(mongoTemplate.count(query, RequestLogModel.class, filterModel.getDbName()));
        Pageable pageable = PageRequest.of(filterModel.getPage(), filterModel.getCount(), sort);
        query.with(pageable);

        List <RequestLogModel> requestLogList = mongoTemplate.find(query, RequestLogModel.class, filterModel.getDbName());
        return requestLogList;
    }

    /**
     * 查询出符合条件的数据
     * @param filterModel
     * @return
     */
    public Integer filterRequestLogRecordCount(RestFilter.RequestLogFilter filterModel) {
        Query query = requestLogCondition(filterModel);
        return (int)mongoTemplate.count(query, RequestLogModel.class, filterModel.getDbName());
    }


    /**
     * 公共的查询条件
     * @param filterModel
     * @return
     */
    private Query requestLogCondition(RestFilter.RequestLogFilter filterModel) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (filterModel.getRequestType() != null) {
            criteria.and("requestType").is(filterModel.getRequestType());
        }
        if (!StringUtil.isEmpty(filterModel.getUriPath())) {
            criteria.and("uriPath").is(filterModel.getUriPath());
        }
        if (!StringUtil.isEmpty(filterModel.getUserId())) {
            criteria.and("userId").is(filterModel.getUserId());
        }
        if (filterModel.getStatus() != null) {
            criteria.and("status").is(filterModel.getStatus());
        }
        query.addCriteria(criteria);
        return query;
    }
}
